Declaration (computer Programming)
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a declaration is a
language construct In computer programming, a language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of the programming language. The term "language construct" is often used ...
specifying identifier properties: it declares a word's (identifier's) meaning."A declaration specifies the interpretation and attributes of a set of identifiers. A ''definition'' of an identifier is a declaration for that identifier that: * for an object ariable or constant causes storage to be reserved for that object; * for a function, includes the function body; * for an enumeration constant, is the (only) declaration of the identifier; * for a typedef name, is the first (or only) declaration of the identifier." C11 specification, 6.7: Declarations, paragraph 5. Declarations are most commonly used for functions, variables, constants, and classes, but can also be used for other entities such as
enumerations An enumeration is a complete, ordered listing of all the items in a collection. The term is commonly used in mathematics and computer science to refer to a listing of all of the elements of a set. The precise requirements for an enumeration (fo ...
and type definitions. Beyond the name (the identifier itself) and the kind of entity (function, variable, etc.), declarations typically specify the data type (for variables and constants), or the
type signature In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is typ ...
(for functions); types may also include dimensions, such as for
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
s. A declaration is used to announce the existence of the entity to the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
; this is important in those strongly typed languages that require functions, variables, and constants, and their types to be specified with a declaration before use, and is used in
forward declaration In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. It is required for a com ...
. The term "declaration" is frequently contrasted with the term "definition", but meaning and usage varies significantly between languages; see below. Declarations are particularly prominent in languages in the
ALGOL ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
tradition, including the BCPL family, most prominently C and
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, and also Pascal.
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mos ...
uses the term "declaration", though Java does not require separate declarations and definitions.


Declaration vs. definition

One basic dichotomy is whether or not a declaration contains a definition: for example, whether a variable or constant declaration specifies its value, or only its type; and similarly whether a declaration of a function specifies the body (
implementation Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy. Industry-specific definitions Computer science In computer science, an implementation is a real ...
) of the function, or only its type signature. Not all languages make this distinction: in many languages, declarations always include a definition, and may be referred to as either "declarations" or "definitions", depending on the language. However, these concepts are distinguished in languages that require declaration before use (for which forward declarations are used), and in languages where interface and implementation are separated: the interface contains declarations, the implementation contains definitions. In informal usage, a "declaration" refers only to a pure declaration (types only, no value or body), while a "definition" refers to a declaration that includes a value or body. However, in formal usage (in language specifications), "declaration" includes ''both'' of these senses, with finer distinctions by language: in C and C++, a declaration of a function that does not include a body is called a
function prototype In computer programming, a function prototype or function interface is a declaration of a function that specifies the function’s name and type signature (arity, data types of parameters, and return type), but omits the function body. While a ...
, while a declaration of a function that does include a body is called a "function definition". In Java declarations occur in two forms. For public methods they can be presented in interfaces as method signatures, which consist of the method names, input types and output type. A similar notation can be used in the definition of
abstract method A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
s, which do not contain a definition. The enclosing class can be instantiated, rather a new derived class, which provides the definition of the method, would need to be created in order to create an instance of the class. Starting with Java 8, the lambda expression was included in the language, which could be viewed as a function declaration.


Declarations and definitions

In the C-family of programming languages, declarations are often collected into
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
s, which are included in other source files that reference and use these declarations, but don't have access to the definition. The information in the header file provides the interface between code that uses the declaration and that which defines it, a form of
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
. A declaration is often used in order to access functions or variables defined in different source files, or in a
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
. A mismatch between the definition type and the declaration type generates a compiler error. For variables, definitions assign values to an area of memory that was reserved during the declaration phase. For functions, definitions supply the function body. While a variable or function may be declared many times, it is typically defined once (in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, this is known as the
One Definition Rule The One Definition Rule (ODR) is an important rule of the C++ programming language that prescribes that classes/structs and non-inline functions cannot have more than one definition in the entire program and template and types cannot have more than ...
or ODR). Dynamic languages such as
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
or
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
generally allow functions to be redefined, that is,
re-bound The Newlydeads were an industrial rock band founded by Faster Pussycat singer Taime Downe. At one time or another, The Newlydeads also featured Taime Downe's Faster Pussycat bandmates: guitarist Xristian Simon, bassist Danny Nordahl, and drumme ...
; a function is a variable much like any other, with a name and a value (the definition). Here are some examples of declarations that are not definitions, in C: extern char example1; extern int example2; void example3(void); Here are some examples of declarations that are definitions, again in C: char example1; /* Outside of a function definition it will be initialized to zero. */ int example2 = 5; void example3(void)


Undefined variables

In some programming languages, an implicit declaration is provided the first time such a variable is encountered at
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concep ...
. In other languages, such a usage is considered to be an error, which may result in a diagnostic message. Some languages have started out with the implicit declaration behavior, but as they matured they provided an option to disable it (e.g.
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
's "use strict" or
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic ( ...
's "Option Explicit").


See also

*
Function prototype In computer programming, a function prototype or function interface is a declaration of a function that specifies the function’s name and type signature (arity, data types of parameters, and return type), but omits the function body. While a ...
*
Scope (programming) In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts ...


Notes


References

{{Reflist, 30em


External links


Declare vs Define in C and C++
Alex Allain

''The C Book,'' GBdirect
Declarations and Definitions (C++)
MSDN *:"Declarations tell the compiler that a program element or name exists. Definitions specify what code or data the name describes." Programming language concepts Articles with example C code